﻿using System.Collections.Generic;
using UnityEngine;

public class ObjectPools : MonoBehaviour
{   
    
    public static ObjectPools mainPools;
    /// <summary>
    /// 总对象池
    /// </summary>
    public List<GameObject> total = new List<GameObject>();//总对象池
    /// <summary>
    /// 以Tag区分的对象池
    /// </summary>
    public Dictionary<string, List<GameObject>> pools = new Dictionary<string, List<GameObject>>();//分Tag的对象池

    void Awake(){
        //设置该对象池为全局对象池
        mainPools = this;
    }

    //基础操作方法
    /// <summary>
    /// 向指定对象池中添加对象
    /// </summary>
    /// <param name="list"></param>
    /// <param name="obj"></param>
    /// <returns></returns>
    public bool Add(List<GameObject> list, GameObject obj){//增加对象
        if(!list.Contains(obj)){
            list.Add(obj);
            return true;
        }else return false;
    }
    /// <summary>
    /// 删除指定对象池中的指定对象
    /// </summary>
    /// <param name="list"></param>
    /// <param name="obj"></param>
    /// <returns></returns>
    public bool Del(List<GameObject> list, GameObject obj){//删除对象
        if(list.Contains(obj)){
            list.Remove(obj);
            return true;
        }else return false;
    }
    
    //直接添加对象
    /// <summary>
    /// 添加对象，对象会自动归类到对象池中
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public bool Add(GameObject obj){
        if(obj.tag != "Untagged"){
            if(!pools.ContainsKey(obj.tag)){
                pools.Add(obj.tag, new List<GameObject>());
            }
            Add(pools[obj.tag], obj);
        }
        return Add(total, obj);
    }
    /// <summary>
    /// 删除对象
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public bool Del(GameObject obj){
        if(obj.tag != "Untagged"){
            if(pools.ContainsKey(obj.tag))Del(pools[obj.tag], obj);
        }
        return Del(total, obj);
    }
}
